#ifndef _BLUETOOTH_REMOTE_H
#define _BLUETOOTH_REMOTE_H

#include <stdint.h> //uint16_t
#include <sys/socket.h>
#include <pthread.h>

//##########################################################
//Bluetooth include file
#include <bluetooth/bluetooth.h> //BTPROTO_HCI
#include <bluetooth/hci.h>          //struct hci_dev_info
#include <bluetooth/hci_lib.h>     //hci_devid()
#include <bluetooth/l2cap.h>      //l2cap
#include <bluetooth/hidp.h>       //hidp

typedef int             BOOL;
typedef signed char     INT8;
typedef signed short    INT16;
typedef signed int      INT32;
typedef unsigned char   UINT8;
typedef unsigned short  UINT16;
typedef unsigned int    UINT32;
typedef float           FLOAT;

#ifndef LPSTR
typedef char*           LPSTR;
#endif
#ifndef DWORD
typedef unsigned long DWORD;
#endif
#ifndef WORD
typedef unsigned short WORD;
#endif

#ifndef VOID
#define VOID void
#endif
#ifndef FALSE
#define FALSE   0
#endif
#ifndef TRUE
#define TRUE    1
#endif
#ifndef NULL
#define NULL    0
#endif

#define DEVICE_MAXIMUM_NUMBER 4

#define L2CAP_PSM_HIDP_CTRL 0x11
#define L2CAP_PSM_HIDP_INTR 0x13

#define DEVICE_NO_READY "00:00:00:00:00:00"
#define DEVICE_MAX_NUM 8
#define CONNECTED_DEVICE_MAX_NUM 4
#define DATA_MAX_NUM 1024



//BT_*  Bluetooth ERROR
//CT_*  Connect ERROR
//SK_*  Socket ERROR
#define BT_SUCCESS 0                                        //Õý³£
#define BT_PROTOCOL_NO_READY 1                  //Ð­ÒéÕ»³ö´í
#define BT_DONGLE_NO_READY 2                       //bluetoothÊÊÅäÆ÷³ö´í
#define BT_SEND_CMD_TO_DONGLE_ERR  3       //ÏòbluetoothÊÊÅäÆ÷·¢ËÍÃüÁîÊ±³ö´í

#define CT_NO_FIND_3DIJOY_CONTROLLER 4   //Ã»·¢ÏÖÊÖ±ú

#define SK_BIND_ERROR  5                                //bind ³ö´í
#define SK_GET_OPT_ERROR    6                          //Get Opt ³ö´í

#define CT_CONNECT_ERROR    7                        //Á¬½ÓÎ´³É¹¦

#define BT_INQUIRY_ERROR    8                          //Inquiry Error

#define CT_NO_CONNECTED_CONTROLLER 9     //µ±Ç°Ã»ÓÐÒÑ¾­Á¬½ÓµÄÊÖ±ú
#define CT_SETTING_CONTROLLER_ERROR 10   //ÉèÖÃÊÖ±úÊ§°Ü

#define SK_READ_ERROR  11  //¶ÁÈ¡´íÎó
#define SK_CLOSE_ERROR 12 //Close Sokcet Error

#define CIRCLESIZE  8

//added
#define BDADDR_LEN					6					//bluetooth device address length
#define BDNAME_LEN					8 //248					//bluetooth device name length
//button define
#define	BUTTON_A					0					//button A
#define	BUTTON_B					1					//button B
#define	BUTTON_MINUS				2					//button MINUS
#define	BUTTON_HOME					3					//button HOME
#define	BUTTON_PLUS					4					//button PLUS
#define	BUTTON_ONE					5					//button 1
#define	BUTTON_TWO					6					//button 2
#define	BUTTON_UP					7					//button UP
#define	BUTTON_DOWN					8					//button DOWN
#define	BUTTON_LEFT					9					//button LEFT
#define	BUTTON_RIGHT				10					//button RIGHT
#define BUTTON_POWER				11					//button POWER
#define BUTTON_C					12					//button C
#define BUTTON_JCENTER				13					//joystick center button

typedef struct bdaddr_socket
{
	bdaddr_t gbdaddr;                      //bdaddr
	int iCSocket;                              //l2cap control socket
	int iDsocket;                              //l2cap data socket
	int used;
	pthread_t ntid;
} bdaddr_socket;

typedef struct GyroCalibration
{
    unsigned short		X0, Y0, Z0;						//12bit加速度校准值
    unsigned short		XG, YG, ZG;						//12bit加速度校准值
    int		GyroX_Cali;
    int     GyroY_Cali;			//陀螺仪校准值
	int		GyroT_Cali;//Temperature;		//温度校准值
	double		GyroXTK_Cali, GyroXTB_Cali;		//x轴温度曲线
	double		GyroYTK_Cali, GyroYTB_Cali;		//y轴温度曲线
}GyroCalibration;

typedef struct AccCalibration
{
    unsigned short		X0, Y0, Z0;						//加速度校准值
    unsigned short		XG, YG, ZG;						//加速度校准值
}AccCalibration;

typedef struct BT_GAME_CONTROLLER_All_DATA
{
	//int Device_index;

	int Key_Left;
	int Key_Right;
	int Key_Up;
	int Key_Down;

	int Key_A;
	int Key_B;

	int Key_Home;
	int Key_X;
	int Key_Y;

	int Act_Hit;

	unsigned short RawGyroX;
	unsigned short RawGyroY;
	unsigned short RawGyroT;

	unsigned short GyroRawAccX;
	unsigned short GyroRawAccY;
	unsigned short GyroRawAccZ;

	unsigned short RawAccX;
	unsigned short RawAccY;
	unsigned short RawAccZ;

	unsigned char RawJsX;
	unsigned char RawJsY;
	unsigned char RawJsB;

} BT_GAME_CONTROLLER_All_DATA;

/*typedef struct BT_FIRST_GAME_CONTROLLER_All_DATA
{
	//int Device_index;

	int Key_Left;
	int Key_Right;
	int Key_Up;
	int Key_Down;

	int Key_A;
	int Key_B;

	int Key_Home;
	int Key_X;
	int Key_Y;

	unsigned short RawGyroX;
	unsigned short RawGyroY;
	unsigned short RawGyroT;

	unsigned short GyroRawAccX;
	unsigned short GyroRawAccY;
	unsigned short GyroRawAccZ;

	unsigned short RawAccX;
	unsigned short RawAccY;
	unsigned short RawAccZ;

	int Move;
	int Power;
} BT_FIRST_GAME_CONTROLLER_All_DATA;*/



typedef struct socket_handle
{
	int iCSocket;
	int iDsocket;
} socket_handle;

///////////////////////////////////////////////////////////////
// added

typedef struct BT_CONNECTION_STATUS
{
	char    cIsconnected;	//connection status (0: disconnected, nonzero: connected)
	char    cRssi;			//signal intension. range -127 -- 127. (normal: 0; weak: negative; strong: positive)
	char    cDuration;		//not available
	unsigned char   ucBatteryLevel;	//battery level
}BT_CONNECTION_STATUS, *lpBT_CONNECTION_STATUS;

typedef struct DEVICE_INFO
{
    UINT8   szDeviceName[BDNAME_LEN];       //bluetooth device name
	UINT8 szDeviceAddr[BDADDR_LEN];       //bluetooth device address
    UINT16   nHardwareVer;							//device hardware version
	UINT16   nSoftwareVer;							//device software version
}DEVICE_INFO, *lpDEVICE_INFO;

//output data
typedef struct
{
	BOOL		Buttons[16];							//button

	struct ACCEL										//acceleration
	{
		BOOL	Enable;
		UINT8   RawX, RawY, RawZ;						//raw data
		FLOAT	X, Y, Z;								//acceleration data, range (-3 -- 3)

		struct ORI										//orientation
		{
			BOOL	Enable;
			FLOAT	Pitch;								//pitch angle, range (-180 -- 180)
			FLOAT	Roll;								//roll angle, range (-180 -- 180)
		}Ori;
	}Acc;

	struct POSI											//position
	{
		BOOL		Enable;
		UINT16 RawX, RawY;							//raw data
		INT32		RelPosX, RelPosY;					//relative position
		INT32		AbsPosX, AbsPosY;					//absolute position, range (0 -- 65535)
		INT32		GyroX_Cali;
        INT32     GyroY_Cali;
	}Mpos;

	struct JSTICK										//joystick
	{
		BOOL		Enable;
		BOOL		CenterBtn;							//center button
		UINT8		RawX, RawY;							//raw data
		FLOAT		X, Y;								//joystick data, range (-1 -- 1)
	}Joystick;

	struct ABTN											//analog button
	{
		BOOL		Enable;
		BOOL		TriggerBtn;							//trigger button
		UINT8		RawBtn;								//raw data
		FLOAT		Btn;								//button data, range (-1 -- 1)
	}Bbtn;												//analog button B
}DEVICE_DATA, *lpDEVICE_DATA;

#ifdef  __cplusplus
extern "C" {
#endif  /* __cplusplus */

//##############################################
// int iSet_Bluetooth_Up()
// when linux reboot. bluetooth Dongle can not use.
//then let it up
//dev_id: device id.(hci0:0      hci1:1)
//Ret:
//	BT_SUCCESS: success
//	BT_PROTOCOL_NO_READY: Not install HCI protocol
//	BT_DONGLE_NO_READY: dev_id Bluetooth device No Ready
//	BT_SEND_CMD_TO_DONGLE_ERR: Send Cmd to bluetooth Dongle Error
//##############################################
int iSet_Bluetooth_Up(uint16_t dev_id);

//##############################################
//int iGet_HID_Socket(int* pCtl_hidp)
//Arguments:
//	pCtl_hidp[out]: get HID socket
//Ret:
//	BT_SUCCESS: success
//	BT_PROTOCOL_NO_READY: Not install HCI protocol
//##############################################
int iGet_HID_Socket(int* pCtl_hidp);

//##############################################
//int iInit_Bluetooth_Module(int* pCtl_hidp);
//Arguments:
//	pCtl_hidp[out]: get HID socket
//	dev_id[in]: device id. default = 0 [First Dongle,Device ID= 0. Second Dongle,Device ID = 1]
//Ret:
//	BT_SUCCESS: success
//	BT_PROTOCOL_NO_READY: Not install HCI protocol
//	BT_DONGLE_NO_READY: dev_id Bluetooth device No Ready
//	BT_SEND_CMD_TO_DONGLE_ERR: Send Cmd to bluetooth Dongle Error
//
//  ³õÊ¼»¯Bluetooth Dongle ºÍÒ»Ð©»ù´¡Êý¾Ý
//  ÇëÔÚ³ÌÐòÆô¶¯Ê±µ÷ÓÃ
//##############################################
int iInit_Bluetooth_Module(int* pCtl_hidp, int dev_id);

//void cmd_L2cap_Connect_All_Remote_Device(int ctl);

//##############################################
//int  iScan_All_Remote_device(int dev_id);
//Arguments:
//	dev_id[in]: Device ID
//Ret:
//	BT_SUCCESS: success
//	BT_PROTOCOL_NO_READY: Not install HCI protocol
//	BT_DONGLE_NO_READY: dev_id Bluetooth device No Ready
//	BT_SEND_CMD_TO_DONGLE_ERR: Send Cmd to bluetooth Dongle Error
//	BT_INQUIRY_ERROR: Inquiry Error
//
//	Scan ÖÜ±ß3DiJoy Controller.²¢°ÑÏà¹ØÐÅÏ¢´æ´¢ÔÚbdaddr_socket_info
//
//	³õÊ¼»¯ºó, Çëµ÷ÓÃ´Ëº¯ÊýScan ÊÖ±ú
//##############################################
int  iScan_All_Remote_device(int dev_id);

//##############################################
//int iConnect_All_Remote_Device_L2cap(int ctl, int dev_id);
//Arguments:
//	dev_id[in]: Device ID
//	ctl[in]: HID Control Socket
//Ret:
//	BT_SUCCESS: success
//	BT_PROTOCOL_NO_READY: Not install HCI protocol
//	BT_DONGLE_NO_READY: dev_id Bluetooth device No Ready
//	BT_SEND_CMD_TO_DONGLE_ERR: Send Cmd to bluetooth Dongle Error
//	CT_NO_FIND_3DIJOY_CONTROLLER: No Find 3Dijoy Controller
//	Á¬½Ó3dijoy Game Controller. ²¢½«Á¬½Ó³É¹¦µÄInfo ·Åµ½Connected_bdaddr_socket_info
//	×î¶àÁ¬½Ó4 ¸öÊÖ±ú


//ÔÚScan 3Dijoy Game Controllerºó,¿ÉÒÔÓÃ´Ëº¯ÊýÁ¬½ÓÖ®.
//##############################################
int iConnect_All_Remote_Device_L2cap(int ctl, int dev_id);


//##############################################
//int iSetting_Game_Controller()
//Arguments:
//
//Ret:
//	BT_SUCCESS: ALL OK
//	CT_NO_CONNECTED_CONTROLLER: µ±Ç°Ã»ÓÐÒÑÁ¬½ÓµÄÊÖ±ú
//	CT_SETTING_CONTROLLER_ERROR: Setting Error
//
//ÔÚÁ¬½ÓÖ®ºóÇëµ÷ÓÃ´Ëº¯Êý
//##############################################
int iSetting_Game_Controller();
int iSetting_Game_Controller_signl(int index);


int do_kill(int ctl, bdaddr_t *bdaddr, uint32_t flags);


//##############################################
//int iGet_Remote_Device_Num();
//Arguments:

//Ret:
//ÒÑÁ¬½ÓÊÖ±ú¸öÊý
//##############################################
int iGet_Remote_Device_Num();

//int index[in]:
//Bluetooth_All_Info* ppInfo[out]

//##############################################
//int iGet_Remote_Device_Num();
//Arguments:
//	int index[in]: ÏëµÃµ½µÄÊÖ±úindex(0-3)
//	Bluetooth_All_Info* ppInfo[out]: µÃµ½µÄÐÅÏ¢
//Ret:
//	BT_SUCCESS: Success
//	SK_READ_ERROR: Read Error
//´Ëº¯Êý»áÈ¥¶ÁÈ¡Êý¾ÝÁ¬½á£¬²¢°Ñ¶ÔÓ¦ÐÅÏ¢·ÅÔÚBluetooth_All_Info
//##############################################
int iGet_Device_All_Info(int index, BT_GAME_CONTROLLER_All_DATA* ppInfo);



//##############################################
//int iClose_Game_Controller(int index);
//Arguments:
//	index[in]: ÏëÒª¶Ï¿ªÊÖ±úµÄindex(0-3)
//Ret:
//	BT_SUCCESS: Success
//	SK_CLOSE_ERROR: Close Socket Error
//##############################################
int iClose_Game_Controller(int index);


//##############################################
//int iGet_Lib_Version();
//Arguments:
//
//Ret:
//	Lib Version.
//##############################################
int iGet_Lib_Version();



//##############################################
//int iClear_Sock_Buf(int index)
//ÓÃÀ´clear socket buf ÖÐÄÚÈÝ¡£ÒòÎªÓÎÏ·¶ÁÈ¡ËÙ¶ÈÍ¨³£
//±ÈÊÖ±úËÙ¶ÈÂýºÜ¶à£¬ÕâÑù¾Í¿ÉÒÔµÃµ½×îÐÂÊý¾Ý
//Arguments:
//	index:¶ÔÓ¦ÊÖ±ú
//Ret:
//	BT_SUCCESS: ALL OK
//##############################################
//int iClear_Sock_Buf(int index);


//##############################################
//int iStart_Read_Data()
//Start read 3dijoy game controller data.
//ÔÚ¿ªÊ¼¶ÁÈ¡Êý¾ÝÖ®Ç°ÐèÒªµ÷ÓÃ´Ëº¯Êý
//Arguments:
//
//Ret:
//	BT_SUCCESS: ALL OK
//	CT_NO_CONNECTED_CONTROLLER: µ±Ç°Ã»ÓÐÒÑÁ¬½ÓµÄÊÖ±ú
//##############################################
int iStart_Read_Data();
int iStart_Read_Data_signl(int index);



//##############################################
//int iGet_Game_Controller_Data(int index)
//Arguments:
//	int index[in]: ÏëµÃµ½µÄÊÖ±úindex(0-3)
//	int* pNum[out]: This time get Game controller data Number
//	BT_GAME_CONTROLLER_All_DATA** ppController_data: Ö¸ÏòÊÖ±úÊý¾Ý×éÍ·µØÖ·µÄÖ¸Õë
//Ret:
//	BT_SUCCESS: Success
//##############################################
//int iGet_Game_Controller_Data(int index, int* pNum, BT_FIRST_GAME_CONTROLLER_All_DATA** ppController_data);


//thread handle function
void *Read_Data_routine(void *arg);

//added

extern int g_dev_id;
extern int g_ctl_hidp;

int iGet_Status(int DeviceIndex);

//new 3DiJoyInterface
BOOL BluetoothDongleReady (); // not implemented
BOOL DeviceInit ();
BOOL SearchDevice (UINT8* DeviceIndex, lpDEVICE_INFO DeviceInfo);
BOOL AbortSearch ();  // not implemented
BOOL CreateConnection(UINT8 DeviceIndex);
BOOL CreateConnection_Addr(UINT8 *DeviceAddr, UINT8 *DeviceIndex);// not implemented
BOOL ReleaseConnection (UINT8 DeviceIndex);
BOOL AbortConnection (UINT8 DeviceIndex); // NOT IMPLEMENTED
BOOL GetConnectionStatus (UINT8 DeviceIndex, lpBT_CONNECTION_STATUS p_Connect_Status);
BOOL ReadDeviceInfo (UINT8 DeviceIndex, lpDEVICE_INFO DeviceInfo);
BOOL SetLEDLevel(UINT8 DeviceIndex, UINT8 LEDIndex, UINT8 BrightLevel);
BOOL SetMotorLevel (UINT8 DeviceIndex, UINT8 MotorIndex, UINT8 Strength);
UINT16 SetAudioFile (UINT8		DeviceIndex,
    LPSTR		Path,
    UINT8		Volume,
    BOOL		Save,
    UINT8*		ID,
    BOOL		Repeat); // not implemented
BOOL SetAudioData ( UINT8 DeviceIndex, UINT8* VoiceData, DWORD Length,
                    UINT8 Volume, BOOL Save, UINT8* ID, BOOL Repeat);// not implemented
BOOL PlayAudioID (UINT8 DeviceIndex, UINT8 ID, UINT8 Volume /*= 0x40*/);// not implemented
BOOL StopAudio (UINT8 DeviceIndex); // not implemented
BOOL ReadRemoteDeviceData (UINT8 DeviceIndex, lpDEVICE_DATA p_Data);
BOOL SetOutputMode (UINT8 DeviceIndex, UINT8 Mode, BOOL Enable);
BOOL EptCheck (UINT8 DeviceIndex, UINT8 CheckDataA, UINT8 CheckDataB, UINT8 *CheckAnswer);  // not implemented
BOOL SetMouseSpeed (UINT8 DeviceIndex, UINT8 Speed);


BOOL CheckJoy(int index); // addition
BOOL WriteAudioBuffer(int DeviceIndex,unsigned int pos);// not implemented, addition


#ifdef  __cplusplus
}
#endif  /* __cplusplus */

#endif

